﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using CarPark.Helper;

namespace CarPark.MainModel
{
    public partial class FrmLogIn : Form
    {
        public bool _DEBUG = true;

        /// <summary>
        /// 构造函数，主窗体不应该close窗体，只能hide，本窗体不嫩决定自己的行为，只能接受用户的输入
        /// 并以事件的形式通知主窗体
        /// </summary>
        /// <param name="parent"></param>
        public FrmLogIn(FrmParent parent)
        {
            InitializeComponent();

            this.MdiParent = parent;
            this.btnLogIn.Click += new System.EventHandler(this.btnLogIn_Click);
            this.cmbCKChoose.DataSource = MidLogin.GetCKNames();
            this.FormClosing += new FormClosingEventHandler(FrmLogIn_FormClosing);

            if (_DEBUG)
            {
                cmbCKChoose.SelectedIndex = 0;
                txtUserName.Text = "patrick";
                txtCKPassword.Text = "123456";
            }
        }

        void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            e.Cancel = true;
            base.OnFormClosing(e);
        }
 
        private void btnLogIn_Click(object sender, EventArgs e)
        {
             if(cmbCKChoose.SelectedIndex < 0)
                MessageBox.Show(GlobalConst.LOGIN_MISSING_CKNAME, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
             else
             {
                 //委托主窗体完成验证
                 (this.MdiParent as FrmParent).loginClick(this, new LogInClickEventArg(cmbCKChoose.SelectedItem.ToString(), txtUserName.Text, txtCKPassword.Text));
             }
        }
    }

    /// <summary>
    /// 登录按钮按下后，向主窗体发送的事件参数类型
    /// 类型包括车库名、用户、密码，主窗体应该对用户
    /// 进行验证，决定下一步如何进行
    /// </summary>
    public class LogInClickEventArg : EventArgs
    {
        public LogInClickEventArg(string ckname , string username , string userpassword)
            : base() 
        {
            CKName = ckname;
            UserName = username;
            UserPassword = userpassword;
        }

        public bool IsLogIn = true;
        public string CKName = string.Empty;
        public string UserName = string.Empty;
        public string UserPassword = string.Empty;
    }
}
